package com.hz.web.controller;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.hz.web.entity.Video;
import com.hz.web.service.IvideoService;


@Controller
@RequestMapping("/video")
@SuppressWarnings("all")
public class VideoController {

	@Resource
	private IvideoService videoService;
	
	//查询具体的一条记录
	@RequestMapping("oneVideo")
	@ResponseBody
	public Object oneVideo(Video entity){
			Video video =new Video(); 
			video=videoService.queryById(entity);
			return video;
	}
	//多条件查询视频
	@RequestMapping(value="queryView",method=RequestMethod.POST)
	@ResponseBody
	public Map queryView(String sort,String indexPage,String number,Integer videoType){
		Map condition = new HashMap();
		int page = indexPage == null ? 1 : Integer.parseInt(indexPage);
		int num = number == null ? 1 :Integer.valueOf(number);
		int minPage = 1;// 最小页数
		condition.put("videoType", videoType);
		int maxNo = videoService.queryCount(condition);
		int maxPage = maxNo%num==0?maxNo/num:(maxNo/num)+1;// 最大页数
		if (page < minPage)
			page = minPage;
		if (page > maxPage)
			page = maxPage;
		int start = num * (page - 1);
		if (start < 0) {
			start = 0;
		}
		condition.put("start", start);
		condition.put("rows", num);
		condition.put("sort",sort);
		Map resultMap=new HashMap();
		List<Video> videols =new ArrayList<Video>();
		videols=videoService.queryView(condition);
		resultMap.put("videols", videols);
		resultMap.put("maxPage", maxPage);
		resultMap.put("indexPage", page);
		return resultMap;
	}
	
	/**
	 * 查询视频信息
	 * @param page
	 * @param rows
	 * @param order
	 * @param sort
	 * @return
	 */
	@RequestMapping(value="query",method=RequestMethod.POST)
	@ResponseBody
	public Map query(int page, int rows){
		Map condition = new HashMap();
		int start = 10 * (page - 1);
		condition.put("start", start);
		condition.put("rows", rows);
		/*condition.put("order", order);
		condition.put("sort", sort);*/
		
		Map map = new HashMap();
		map.put("rows", videoService.query(condition));
		map.put("total", videoService.queryCount(condition));
		return map;
	}
	
	/**
	 * 添加
	 * @param video
	 * @param session
	 * @return
	 */
	@RequestMapping(value="addVideo",method=RequestMethod.POST)
	@ResponseBody
	public Object addVideo(Video video,HttpSession session){
		int r=0;
		try {
			r=videoService.add(video,session);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return r;
	}
	
	/**
	 * 修改
	 * @param video
	 * @param session
	 * @return
	 */
	@RequestMapping(value="updateVideo",method=RequestMethod.POST)
	@ResponseBody
	public Object updateVideo(Video video,HttpSession session){
		int r=0;
		try {
			r=videoService.update(video,session);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return r;
	}
	
	//删除用户
	@RequestMapping(value="delVideo",method = RequestMethod.POST)
	@ResponseBody
	public Object delVideo(Integer videoId,Integer videoStatus){
		int r=0;
		Video record = new Video();
		record.setVideoId(videoId);
		record.setVideoStatus(videoStatus);
		try {
			r=videoService.del(record);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return r;
	}
	//点击增加观看次数
	@RequestMapping(value="addClicks",method=RequestMethod.POST)
	@ResponseBody
	public Object addClicks(Video entity,HttpSession session){
		Video video = videoService.queryById(entity);
		int r=0;
		try {
			video.setVideoClicks(video.getVideoClicks()+1);
			r=videoService.update(video,session);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return r;
	}
}
